package com.xyl.controller;

import com.xyl.commons.ApiResult;
import com.xyl.entity.Activity;
import com.xyl.services.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("activity")
public class ActivityController extends BaseController {

    @Autowired
    private ActivityService activityService;

    @RequestMapping
    public String activityIndex(Model model){
        List<Activity> list =  activityService.getByUserId(user.getUserId());
        model.addAttribute("activities",list);
        return "activity";
    }

    @PostMapping("add")
    @ResponseBody
    public ApiResult add(@RequestBody Activity activity){
        activity.setUserId(user.getUserId());
        activity.setCreateTime(new Date());
        int count = activityService.add(activity);
        return resultIsOne(count);
    }

    @PostMapping("remove")
    @ResponseBody
    public ApiResult remove(@RequestParam Integer id){
        int count = activityService.removeByAId(id);
        return resultIsOne(count);
    }

    @PostMapping("modify")
    @ResponseBody
    public ApiResult modify(@RequestBody Activity activity){
        activity.setUserId(user.getUserId());
        activity.setUpdateTime(new Date());
        int count = activityService.modify(activity);
        return resultIsOne(count);
    }

    @GetMapping("filter")
    public String filter(Model model,String type,Integer year){
        List<Activity> list = activityService.filterByTypeAndYear(user.getUserId(),type,year);
        model.addAttribute("activities",list);
        return "activity";
    }
}
